package attori.controller;

import sde.actor.Actor;
import sde.actor.Message;
import sde.actor.Timer;
import attori.attuatori.AttuatorePortaIF;

public class ControllerPorta extends Actor implements ControllerPortaIF {
	public static final byte CREATED = 0, APERTA = 1, CHIUSA = 2;
	private AttuatorePortaIF porta;
	private long delay;
	Timer t = new Timer();

	public ControllerPorta() {
		super();
		become(CREATED);
	}

	@Override
	protected void handler(Message m) {
		switch (currentStatus()) {
		case CREATED:
			if (m instanceof Init) {
				porta = ((Init) m).ap;
				delay = ((Init) m).delay;
			}
		case CHIUSA:
			if (m instanceof Notify) {
				porta.send(new AttuatorePortaIF.ToOpen());
				t.set(new Idle(), this, delay);
				become(APERTA);
			}
			break;
		case APERTA:
			if (m instanceof Idle) {
				porta.send(new AttuatorePortaIF.ToClose());
				become(CHIUSA);
			}
			if (m instanceof Notify) {
				t.reset();
				t.set(new Idle(), this, delay);
			}
			break;
		default:
		}
	}

}
